package cn.dogger.easyboot.demo.controller;

import cn.dogger.easyboot.log.annotation.Slf4j;
import cn.dogger.easyboot.log.utils.Log;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

import java.util.concurrent.TimeUnit;

/**
 * @author dogger
 * 2022/3/19 22:35
 */
@RestController
@Slf4j
public class DemoController {


    @GetMapping("demoAction")
    @ApiOperation("demoAction")
    public void demoAction(){

    }

    /**
     * 阻塞5秒钟
      */
    public String createStr() {
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException ignored) {
        }
        return "some string";
    }

    /**
     * 普通的SpringMVC方法
     *  从上到下执行返回
     */
    @GetMapping("/1")
    public String get1() {
        Log.info("get1 start");
        String result = createStr();
        Log.info("get1 end.");
        return result;
    }

    /**
     * WebFlux(返回的是Mono)
     * 异步处理createStr方法，相当于开了个线程。结果：先返回log日志再return页面结果
     */
    @GetMapping("/2")
    public Mono<String> get2() {
        Log.info("get2 start");
        Mono<String> result = Mono.fromSupplier(this::createStr);
        Log.info("get2 end.");
        return result;
    }

}
